#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.c>

void adddept();
void deletedept();
void editemp();
void addemp();
void addnewtelephone();
void enqdeptcode();
void enqempname();
int check();//for y or n or invalid input

typedef struct
{char deptname[16];
 int deptcode;
}deptrecord;
//department records are store in file name -dept.txt

deptrecord readfile_dept(deptrecord d,FILE *);
void writefile_dept(deptrecord d,FILE *);

typedef struct
{int empid ;
 char empname[26];
 int depcode ;
 char location[6];
 int teleno;
}emprecord;


emprecord readfile_emp(emprecord e,FILE *);
void writefile_emp(emprecord e,FILE *);


char c;//to check for 'y' or 'n'

//employee records are store in file name -emp.txt

int main()
{ int ch,ch1;

  do{ clrscr();
      printf("**MAIN MENU**\n");
      printf("1.Department Maintenance\n");
      printf("2.Employee Master Maintenance\n");
      printf("3.Directory Maintenance\n");
      printf("4.Exit\n");
      printf("Enter your choice:");
      scanf("%d",&ch);
      switch(ch)
      {case 1: 
              
               do{ clrscr();
                   printf("**Department Maintenance Screen**\n");
                   printf("1.Add a Department\n");
                   printf("2.Delete a Department\n");
                   printf("0.Go back to Main Menu\n");           
                   printf("Enter your choice:");
                   scanf("%d",&ch1);
                   switch(ch1)
               	    { case 1: adddept();
                              break;
                      case 2: deletedept();
                              break;
                      case 0: break;
                
                     default: printf("Invalid input\n");
                              getch();
                              break;
                                   
                   }
                }while(ch1!=0);

                break;
       case 2: 
               do{ clrscr();
                   printf("**Employee Master Screen**\n");
                   printf("1.Add Employees\n");
                   printf("2.To Edit Employee Information\n");
                   printf("0.Go back to Main Menu\n");           
                   printf("Enter your choice:");
                   scanf("%d",&ch1);
                   switch(ch1)
                   {case 1:  addemp();
                             break;
                    case 2:  editemp();
                             break;
                    case 0:  break;
              
                   default:  printf("Invalid input\n");
                             getch();
                             break;
                                   
                  }
                }while(ch1!=0);

               break;
       case 3: 
               do{ clrscr();
                   printf("**Directory Maintenance Screen**\n");
                   printf("1.Add Telephone Numbers\n");
                   printf("2.To query on name of telephone owner(Employee)\n");
                   printf("3.To query on Department.\n");
                   printf("0.Go back to Main Menu\n");           
                   printf("Enter your choice:");
                   scanf("%d",&ch1);
                   switch(ch1)
                   {case 1: addnewtelephone();
                            break;
                    case 2: enqempname();
                            break;
                    case 3: enqdeptcode();
                            break;
                    case 0: break;
              
                  default : printf("Invalid input\n");
                            getch();
                            break;
                                   
                  }
                }while(ch1!=0);

               break;
       case 4: printf("\nBYE");
               getch();   
               clrscr();
               break;
      default: printf("Invalid input\n");
               getch();
               clrscr();
               break;
                                   
      }
    }while(ch!=4);
           
           
 
  return 0;
}

// read & write file for department

void writefile_dept(deptrecord d,FILE *fp)
{ fprintf(fp,"%s %d ",d.deptname,d.deptcode);
// fprintf(fp,"%s\n",d.deptname);
 //fprintf(fp,"%d\n",d.deptcode);

}

deptrecord readfile_dept(deptrecord d,FILE *fp)
{fscanf(fp,"%s %d ",d.deptname,&d.deptcode);
//fscanf(fp," %[^\n]",d.deptname);
// fscanf(fp," %d",&d.deptcode);
 return(d);
}


//read & write file for employee


void writefile_emp(emprecord e,FILE *fp)
{fprintf(fp,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);   
 /*fprintf(fp,"\n%d",e.empid);
 fprintf(fp,"\n%s",e.empname);
 fprintf(fp,"\n%d",e.depcode);
 fprintf(fp,"\n%s",e.location);
 fprintf(fp,"\n%d",e.teleno);
*/
}

emprecord readfile_emp(emprecord e,FILE *fp)
{fscanf(fp,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);             
/*   fscanf(fp," %d",&e.empid);
  fscanf(fp," %[^\n]",e.empname);
  fscanf(fp," %d",&e.depcode);
  fscanf(fp," %[^\n]",e.location);
  fscanf(fp," %d",&e.teleno);*/
 return(e);
}
 

int check()
{ 
 do{fflush(stdin);
    scanf("%1s",&c); 
    
    if(toupper(c)=='N')        
      return 0;
    if(toupper(c)!='Y')
       printf("\nInvalid input press[y/n]: ");     
    }while(toupper(c)!='Y');
      
    return 1;
}    



//  add department --Department Maintenance
void adddept()
{
 deptrecord d;
 FILE *fp;
 char temp[16];
 
 int flag=1,flag1=0;

 d.deptcode=999;
 do{ flag=1;
     flag1=0;  
     do{clrscr();
        printf("Add a Department \n\n");
    
        do{
           printf("\nEnter Department name:");
           scanf("%s",temp); 
           if(strlen(temp)>15)
             {printf("\nDepartment name should be not exceed 15 character");
              getch();
              clrscr();
             } 
           else
             flag1=1;
          }while(flag1==0);  //loop till proper department name(not exceeding 15 character) is given
                              
  
        if(((fp=fopen("dept.txt","r"))!=NULL))//dept.txt must exist
         {
           while(!feof(fp)) //scan till the end of file
            {d=readfile_dept(d,fp);//fscanf(fp,"%s %d ",d.deptname,&d.deptcode);
             //temp name should be same as department name (comparison is case insensitive) 
             flag=strcmpi(temp,d.deptname);
             if(flag==0)
               {printf("\nName already exist.\n");
                getch(); 
                clrscr();
                break;   
               }  
             } 
           fclose(fp);
         }
 
      }while(flag==0);// //loop till proper department name(not which already exist) is given
   
    strcpy(d.deptname,temp);
    fp=fopen("dept.txt","a+");
    
    d.deptcode+=1;//new department code is one more than the highest code which currently exist.
    writefile_dept(d,fp);//fprintf(fp,"%s %d ",d.deptname,d.deptcode);
    fclose(fp); 
    printf("\n\nDepartment Code:%d",d.deptcode);
    printf("\n\nAdd another Department[y/n]:");
   
      
      
    }while(check());
   //loop ,while user wants to continue adding                  


}


 //add employee (employee master )
void addemp()
{deptrecord d;
 emprecord e,temp;
 FILE *fpe,*fpd; //fpe for emprecord & fpd for department record
 int flag=0;
 int flag1;
 
 temp.empid=999; 
 
 
 do{ clrscr();
     printf("\nAdd an Employee\n\n");
     flag1=0;
     do{ printf("\nEnter the name :");
         scanf("%s",e.empname);
         if(strlen(e.empname)>25)
           {printf("\nemployee name should be not exceed 25 character");
            getch();
            clrscr();
           } 
 
                 else
          flag1=1;
       }while(flag1==0);//loop till proper employee name(not exceeding 25 character) is given  
 
      if(((fpe=fopen("emp.txt","r"))!=NULL))//emp.txt must exist
         {  while (!feof(fpe))
                 temp=readfile_emp(temp,fpe);
                 
          
                   
           fclose(fpe);
         }
     e.empid=temp.empid+1;//new department code is one more than the highest code which currently exist.
     printf("\n\nEmployee Id :%d",e.empid);
  
     if((fpd=fopen("dept.txt","r"))==NULL)
                {printf("\nFile cannot be open")   ;
                 getch();
                 clrscr();
                 return;         
                 }
         
      printf("\n\nDEPARTMENT NAME\tDEPARTMENT CODE\n" ) ; 
    
      while(!feof(fpd))
            { d=readfile_dept(d,fpd);
              printf("\n%s\t\t%d",d.deptname,d.deptcode);
                         
            }
      fclose(fpd);                     
      do{flag=0;
         printf("\n\nEnter Department Code:");
         scanf("%d",&e.depcode);
         fpd=fopen("dept.txt","r");
                
         while(!feof(fpd))
               {d=readfile_dept(d,fpd);
                   //employee with that department code,then display details     
                 if(d.deptcode==e.depcode)
                   {                          
                   printf("\nDepartment Name=%s",d.deptname);
                   flag=1;
                   break;
                             
                   }
                         
                }                                           
 
          fclose(fpd);
         }while(flag!=1);// loop till department code given exist
     
     //Entering other details
      flag1=0;                   
     do{
        printf("\nLocation:");
        scanf("%s",e.location);
        if(strlen(e.location)>5)
          {printf("\nLocation should be not exceed 5 character");
           getch();
           clrscr();
          } 
        else
          flag1=1;
       }while(flag1==0);// loop till proper location is given 
                   
         
    fpe=fopen("emp.txt","a+");
    //telephone no.is initially assign 0 it is assign proper value in another module
    e.teleno=0;
    writefile_emp(e,fpe);
    fclose(fpe); 
    printf("\nAdd another employee[y/n]: ");
   
      
  }while(check());
     //loop ,while user wants to continue adding   


}  
 


 
//Edit employee table in employee master 

void editemp()
{deptrecord temp1;
 emprecord e,temp;
 FILE *fpe,*fpnew; //fpe for emprecord  & fpnew for new record 
 int flag,ch;
 
  
 do{ 
           
    do{ flag=0;
        clrscr();
        printf("\nEdit Employee Information\n\n");
        printf("\nEnter the Employee Id :");
        scanf("%d",&e.empid);
 
        if( (fpe=fopen("emp.txt","r"))==NULL)
          {printf("\nFile cannot be open")   ;
           getch();
           clrscr();
           return;         
          }
        else   
          { while (!feof(fpe))
               {temp=readfile_emp(temp,fpe);
                //fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);             

 
                 if(e.empid==temp.empid)
                  { e.depcode=temp.depcode;
                    strcpy(e.empname,temp.empname);
                    strcpy(e.location,temp.location);
                    e.teleno=temp.teleno;
                    flag=1;
                    break;   
                  }
              } 
            fclose(fpe);
            if(flag==0)
              { printf("\nEmployee id  does not exist")   ;       
                printf("\nDo you want to enter  another id[y/n]:")   ;
                if(!check())
                  return; 
               
               }
               
          }             
                               
                                    
                                           
                                                  
       }while(flag==0);    
       
                                                                
     //Entering new details                                   
      printf("\nCurrent Department is  :%d",e.depcode);
      flag=0;
      do{
         printf("\nEnter the new  Department  :");
         scanf("%d",&e.depcode);
         fpe=fopen("dept.txt","r");
         while (!feof(fpe))
            { temp1=readfile_dept(temp1,fpe); 
          //  fscanf(fpe,"%s %d ",temp1.deptname,&temp1.deptcode);
                                                   
                                        
              if(e.depcode==temp1.deptcode)
                {
                 flag=1;//to check whether entered department exist
                 break;   
                }
            } 
             
         fclose(fpe);
         if(flag==0)
           {printf("Department code does not exist");
            getch();
            clrscr();
           }
        }while(flag==0);                                                                   
    
      printf("\nThe current Location:  %s",e.location);
      flag=0;
      do{
         printf("\nNew Location:");
         scanf("%s",e.location);
         if(strlen(e.location)>5)
          { printf("\nLocation should be not exceed 5 character");
            getch();
            clrscr();      
          }     
         else
            flag=1;
        }while(flag==0);  
          
      
 
       // adding edited info. for that emp id  
      // in employee.text
     fpe=fopen("emp.txt","r"); 
     fpnew=fopen("temp.txt","w");//temp text file where updated information is kept
     while(!feof(fpe))
          {temp=readfile_emp(temp,fpe); 
          //fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);
             if(e.empid==temp.empid)
                    {  writefile_emp(e,fpnew);
                    }
                   else
                      writefile_emp(temp,fpnew);    
           /* if(e.empid==temp.empid)
               { fprintf(fpnew,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);   
               }
            else
               fprintf(fpnew,"%d %s %d %s %d ",temp.empid,temp.empname,temp.depcode,temp.location,temp.teleno);               
*/
              
         }     
     fclose(fpe);
     fclose(fpnew);             
    //copying  from temp to old file emp.txt so that now it has the new version  
     fpe=fopen("emp.txt","w");
     fpnew=fopen("temp.txt","r");
     while (!feof(fpnew))
          { //fscanf(fpnew,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);
            e=readfile_emp(e,fpnew);
            writefile_emp(e,fpe);  
            //fprintf(fpe,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);   
                             
          }     
     fclose(fpe);
     fclose(fpnew);      
 

     printf("\n Do you want to edit another employee[y/n])")   ;
     
    }while(check());
          

 
}




 //delete Department (Department Maintenance) 

void deletedept()
{

 //delete Department (Department Maintenance) 
 deptrecord d;
 emprecord e;
 FILE *fpe,*fpd,*fpnew; //fpe for emprecord & fpd for department record & fpnew for new record 
 int temp; //tempcode
 char c;
 int flag;

 
 do{
     clrscr();
     printf("\nDelete a department\n\n");
     do{ flag=0;
         printf("\nEnter the department code:");
         scanf("%d",&temp);
 
        if( (fpd=fopen("dept.txt","r"))==NULL)
          {printf("\nFile cannot be open")   ;
           getch();
           clrscr();
           return;         
          }
        else   
          { while (!feof(fpd))
            {d=readfile_dept(d,fpd);
            //fscanf(fpd,"%s %d ",d.deptname,&d.deptcode);
             
             if(d.deptcode==temp)
               {  flag=1;
                  break;   
                }
             } 
           fclose(fpd);
           if(flag==0)
             { printf("\nCode does not exist")   ;       
               printf("\nDo you want to delete another department[y/n]")   ;
              if(!check())
                  return; 
              
             }
               
          }             
                             
                                    
                                                                                  
        }while(flag==0);
                                                               
     printf("\nDetails");                                                                     
     printf("\nName\t:%s",d.deptname);  
     printf("\nEmployees in the Department\n\n ");                                                                            
     if( (fpe=fopen("emp.txt","r"))==NULL)
         {printf("File cannot be open")   ;
          getch();
          clrscr();
          return;         
         }
     else   
        {
            printf("\nEmployee Name\tLocation\tTelephone NO.\n" );
        
            while (!feof(fpe))
                 { e=readfile_emp(e,fpe);
                  //fscanf(fpe,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);
                  
                  if(e.depcode==temp)
                    {  printf("\n%d ",temp);     
                    printf("\n%s \t %s\t %d",e.empname,e.location,e.teleno);  
                    }
             
                }                                                                        
                                                                                                                              

                                                                                                                              

         }
        
    fclose(fpe);                                                                                                              

                                                                                                                              

    
    printf("\nDo you want to delete [y/n]:");
    if(!check())
        return; 
   
            
   
      // Deleting records with that department code   in department file
     // in deptnew.text is file which will not contain the deleted informations 
    temp=d.deptcode;
              
    fpd=fopen("dept.txt","r"); 
    fpnew=fopen("deptnew.txt","w");
    while (!feof(fpd))
           {d=readfile_dept(d,fpd);
            //fscanf(fpd,"%s %d ",d.deptname,&d.deptcode);
            if(d.deptcode!=temp)
                { writefile_dept(d,fpnew);
                // fprintf(fpnew,"%s %d ",d.deptname,d.deptcode);  
                }
             
           }     
   fclose(fpd);
   fclose(fpnew);             
   //copying  new to old file
   fpd=fopen("dept.txt","w");
   fpnew=fopen("deptnew.txt","r");
   while (!feof(fpnew))
                 {d=readfile_dept(d,fpnew);
                  // fscanf(fpnew,"%s %d ",d.deptname,&d.deptcode);
                   writefile_dept(d,fpd);
                   //fprintf(fpd,"%s %d ",d.deptname,d.deptcode);                
                 }     
   fclose(fpd);
   fclose(fpnew);      
 
 
 
      // Deleting records with that department code  
     // in employee.text
   fpe=fopen("emp.txt","r"); 
   fpnew=fopen("deptnew.txt","w");
   while (!feof(fpe))
                 { //fscanf(fpe,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);
                   e=readfile_emp(e,fpe);
                   if(e.depcode!=temp)
                    {// fprintf(fpnew,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);   
                    writefile_emp(e,fpnew);
                    }
             
                }     
   fclose(fpe);
   fclose(fpnew);             
   //copying  new to old file
   fpe=fopen("emp.txt","w");
   fpnew=fopen("deptnew.txt","r");
   while (!feof(fpnew))
                 {e=readfile_emp(e,fpnew);
                 // fscanf(fpnew,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);     
                  // fprintf(fpe,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);                
                   writefile_emp(e,fpe);
                 }     
   fclose(fpe);
   fclose(fpnew);      
 
 
   printf("\nDelete another department[y/n]")   ;

  }while(check());
 
 
}
 

//add new telephone no. (Directory Maintenance) 
void addnewtelephone()
{
 emprecord e,temp;
 FILE *fpe,*fpnew; //fpe for emprecord  & fpnew for new record 
 char c;
 
 int flag;
 
 do{ clrscr();
     printf("Add a Telephone Number\n\n");
    
 
     do{ clrscr();
         printf("Add a Telephone Number\n\n");
     
         flag=0;
         printf("\nEnter Employee id :");
         scanf("%d",&e.empid);
 
         if( (fpe=fopen("emp.txt","r"))==NULL)
            {printf("\nFile cannot be open")   ;
             getch();
             clrscr();
             return;         
            }
         else   
            {  while(!feof(fpe))
                  { //fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);          
                    temp=readfile_emp(temp,fpe);
                    if(e.empid==temp.empid)
                        {  e.depcode=temp.depcode;
                           strcpy(e.empname,temp.empname);
                           strcpy(e.location,temp.location);
                           flag=1;
                           break;   
                        }
                   } 
         fclose(fpe);
         if(flag==0)
          { printf("\nEmployee id  does not exist")   ;       
            printf("\nDo you want to enter  another id[y/n]")   ;
            if(!check())
                 return;
                 
           }
               
        }             
                             
                                                                                                                   
      }while(flag==0);                                                     
                                                                
     e.teleno=0;                                                                     
     printf("\nLocation :%s",e.location);
     printf("\nDepartment code :%d",e.depcode);
    
 
     // allocate 7 digit telephone no.                                                                         
    
     fpe=fopen("emp.txt","r");
     while(!feof(fpe)) 
           {// fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);              
             temp=readfile_emp(temp,fpe);
             if((e.depcode==temp.depcode)&&(temp.teleno!=0))
               { if(e.teleno<=temp.teleno)
                    e.teleno=temp.teleno+1; 
              //when there are is an employee of that department with a telephone no. assign 
              //to it. then new no. assign to employee is one higher than the current highest
                }
             
           }  
      fclose(fpe);
      if(e.teleno==0)
        {e.teleno=e.depcode*1000 + 1;
        //when there are no employee of that department with a telephone no. assign to it
        }           
      //first 4 digit is department code.
      //next 3 digit are 001,002....
      printf("\nThe telephone number allocated :%d",e.teleno);
   
   
      // adding telephone no. for that emp id  
     // in employee.text
     fpe=fopen("emp.txt","r"); 
     fpnew=fopen("temp.txt","w");
     while (!feof(fpe))
             { //fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);
                temp=readfile_emp(temp,fpe); 
               if(e.empid==temp.empid)
                 {// fprintf(fpnew,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);   
                  writefile_emp(e,fpnew);
                  }
               else
                    writefile_emp(temp,fpnew);
             //       fprintf(fpnew,"%d %s %d %s %d ",temp.empid,temp.empname,temp.depcode,temp.location,temp.teleno);          

                   
             }     
     fclose(fpe);
     fclose(fpnew);             
     //copying  new to old file
     fpe=fopen("emp.txt","w");
     fpnew=fopen("temp.txt","r");
     while (!feof(fpnew))
            {// fscanf(fpnew,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);
             // fprintf(fpe,"%d %s %d %s %d ",e.empid,e.empname,e.depcode,e.location,e.teleno);              
              e=readfile_emp(e,fpnew);
              writefile_emp(e,fpe);   
             }     
    fclose(fpe);
    fclose(fpnew);      
 
    printf("\nAdd another number[y/n]")   ;
   
  }while(check());
 
      
   
 }
 
  
   
 
//Enquiry on department code  directory maintenance 
void enqdeptcode()
{emprecord e,temp;
 FILE *fpe; //fpe for emprecord  & fpnew for new record 
 int count,ch=0;
 int flag;
 char c;
 
  do{  clrscr();
       printf("\t\tTelephone Enquiry by Department Code\n\n");
  
       do{ clrscr();
           printf("\t\tTelephone Enquiry by Department Code\n\n");
    
           flag=0;
           count=1;
           printf("\nEnter Department Code:");
           scanf("%d",&e.depcode);
           printf("\nDetails\n");
           printf("\nName \tLocation \tTelephone No.\n\n");
      
            
           if( (fpe=fopen("emp.txt","r"))==NULL)
             {printf("File cannot be open")   ;
              getch();
              clrscr();
              return;         
             }
          else   
             { while (!feof(fpe))
                    {//fscanf(fpe,"%d %s %d %s %d ",&temp.empid,temp.empname,&temp.depcode,temp.location,&temp.teleno);         
                    temp=readfile_emp(temp,fpe);
     
                     if(e.depcode==temp.depcode)
                        {  if(count<6) 
                              { printf("\n%s\t%s\t\t%d",temp.empname,temp.location,temp.teleno);
                                count++;
                              }
                           flag=1;
                           break;   
                         }
                     } 
               fclose(fpe);
          
               if(flag==0)
                 { printf("\nDepartment code does not exist")   ;       
                   printf("\n\nDo you want to enter  another code[y/n])")   ;
                   if(!check())
                     return;
                 }
               
            }             
                             
                                    
                                           
                                                  
       }while(flag==0);                                                     
                                                                
      printf("\n Do you want to enquire about another department code[y/n]")   ;
      
   }while(check());
 

 
 }

   
  
//Enquiry on details for a particular employee --  directory maintenance 
void enqempname()
{deptrecord temp1;
 emprecord e;
 FILE *fpe; //fpe for emprecord  & fpnew for new record 
 char sname[26];//name for which details are to be search.
 char test1,test2;// characters used for comparison 
 int len_sname,len_empname;//length of the names
 int flag,flag1,f;
 int i,j;
 char c;
 do{ 
     do{   
        clrscr();
        printf("\t\tEnquiry by Employee Name \n\n");
    
        printf("\nEnter Employee Name:");
        scanf("%s",sname); 
        len_sname=strlen(sname);
        printf("\n\nDetails");
        printf("\nName\tLocation\tDepartment Code\t\tTelephone No.");
        flag1=0; 
            
        if((fpe=fopen("emp.txt","r"))==NULL)
           {printf("\nFile cannot be open")   ;        
            getch();
            clrscr();
            return;         
           }
        else   
           { while(!feof(fpe))
               {e=readfile_emp(e,fpe);
               //fscanf(fpe,"%d %s %d %s %d ",&e.empid,e.empname,&e.depcode,e.location,&e.teleno);              
                len_empname=strlen(e.empname);
    
                for(i=0;i<=len_empname-len_sname;i++)
                  {flag=1; 
                   for(j=0;j<len_sname;j++)
                      {test1=toupper(e.empname[i+j]);
                       test2=toupper(sname[j]);
                       if(test1!=test2)
                         {flag=0;
                          break;
                         }  
                     }
                  if(flag==1)
                     {printf("\n%s\t%s\t\t%d\t\t\t%d ",e.empname,e.location,e.depcode,e.teleno);              
                      flag1=1;//to show name exist
                      break;  
                     } 
               }
           } 
       fclose(fpe);
          
        if(flag1==0)
          { printf("\nName does not exist")   ;       
            printf("\nDo you want to enter  another name[y/n]")   ;
            if(!check())
        return;
            
          }   

                         
        }
       }while(flag1==0);     
                   
                             
                                    
                                           
                                                  
                                                           
                                                                
   printf("\n Do you want to enquire about another name[y/n]")   ;
   
    }while(check());
    
 
 }    
 



 
        



